// 获取应用实例
const app = getApp();

Page({
  data: {
    statistics: {
      totalItems: 0,
      todayItems: 0,
      masteredItems: 0
    },
    todayList: [],
    historyList: []
  },

  onLoad() {
    this.loadPageData();
  },

  onPullDownRefresh() {
    this.loadPageData().then(() => {
      wx.stopPullDownRefresh();
    });
  },

  // 加载页面数据
  loadPageData() {
    return Promise.all([
      this.loadStatistics(),
      this.loadTodayList(),
      this.loadHistoryList()
    ]);
  },

  // 加载统计信息
  loadStatistics() {
    return app.request({
      url: '/memory-curve/statistics',
      method: 'GET'
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          statistics: res.data
        });
      }
    }).catch(() => {
      app.showError('获取统计信息失败');
    });
  },

  // 加载今日复习列表
  loadTodayList() {
    return app.request({
      url: '/memory-curve/today',
      method: 'GET'
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          todayList: res.data.list
        });
      }
    }).catch(() => {
      app.showError('获取今日复习列表失败');
    });
  },

  // 加载复习历史
  loadHistoryList() {
    return app.request({
      url: '/memory-curve/history',
      method: 'GET'
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          historyList: res.data.list
        });
      }
    }).catch(() => {
      app.showError('获取复习历史失败');
    });
  },

  // 跳转到复习
  goToReview(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/memory-curve/review/review?id=${id}`
    });
  }
}); 